<?php

namespace app\official\controller;

use think\Controller;
// 微信公众号封装类
use EasyWeChat\Factory;
// 微信公众号配置
use addons\wechat\library\Config;

class Template extends Controller
{
    // 微信属性
    protected $WeChat = null;

    public function __construct()
    {
        parent::__construct();

        $this->WeChat = Factory::officialAccount(Config::load());
    }

    // 手动发送模板信息
    public function template()
    {
        if ($this->request->isPost()) {
            $code = $this->request->param('code', '', 'trim');
            $title = $this->request->param('title', '', 'trim');
            $price = $this->request->param('price', '', 'trim');

            $result = $this->WeChat->template_message->send([
                'touser' => 'oPPYU6C6c7QljRazXJ_Y8IAphHMs',
                'template_id' => 'f46BDEgHOC4Xo9xLbJgfRampQ8g_6X3vQ_5JU256D6E',
                'data' => [
                    'first' => '购买课程成功',
                    'code' => $code,
                    'title' => $title,
                    'price' => $price,
                    'createtime' => date('Y-m-d H:i', time())
                ],
            ]);

            if ($result) {
                echo '发送成功';
            } else {
                echo '发送失败';
            }
        }

        return $this->fetch();
    }
}
